//
//  LWMarketViewController.m
//  LWShopItem
//
//  Created by mac on 17/3/17.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWMarketViewController.h"
#import "UIColor+Addition.h"
#import "Masonry.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import "LWbigCategoryModel.h"
#import "LWsmallCategoryModel.h"
#import "LWorderListViewController.h"


@interface LWMarketViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>

//左侧分类视图数组
@property(nonatomic,strong)NSMutableArray *arrayM;

//右侧分类视图
@property (nonatomic,strong)NSMutableArray *smallArrayM;

/** 左侧分类的表格 */
@property (nonatomic, strong) UITableView *leftClassTableView;

/** 右侧分类的表格 */
@property (nonatomic, strong) UITableView *rigthClassTableView;

/** 记录选中的分类的index */
@property (nonatomic, assign) NSUInteger categoryIndex;


@end

@implementation LWMarketViewController
- (NSMutableArray *)arrayM
{
    if (!_arrayM) {
        
        self.arrayM = [[NSMutableArray alloc]init];
        
    }
    return _arrayM;
}

- (NSMutableArray *)smallArrayM
{
    if (_smallArrayM) {
        self.smallArrayM = [[NSMutableArray alloc]init];
    }
    return _smallArrayM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:0x9ACD32];
    
    [self setupUI];
    [self loadData];
    
    
}

#pragma mark - UI
- (void)setupUI
{
    //左侧分类列表视图
    UITableView *leftClassify = [[UITableView alloc]init];
    self.leftClassTableView = leftClassify;
    leftClassify.delegate = self;
    leftClassify.dataSource = self;
    leftClassify.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:leftClassify];
    
    [leftClassify mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view);
        
        make.top.equalTo(self.view);
        
        make.width.mas_equalTo(120);
        
        make.bottom.equalTo(self.view).offset(-46);
        
    }];
    
    
    //右侧菜品详情列表
    UITableView *rigthClassify = [[UITableView alloc]init];
    self.rigthClassTableView = rigthClassify;
    rigthClassify.delegate = self;
    rigthClassify.dataSource = self;
    rigthClassify.backgroundColor = [UIColor colorWithHex:0xF2F2F2];
    
    [self.view addSubview:rigthClassify];
    
    [rigthClassify mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(64);
        
        make.bottom.equalTo(self.view);
        
        make.right.equalTo(self.view);
        
        make.left.equalTo(leftClassify.mas_right);
        
    }];
    
}
#pragma MARK - 加载数据
- (void)loadData
{
    AFHTTPSessionManager  *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://shop.sdlinwang.com/index.php?m=Api&c=Goods&a=goodsCategoryList" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        NSLog(@"请求成功JSON:%@", JSON);
        
        self.arrayM = [LWbigCategoryModel mj_objectArrayWithKeyValuesArray:JSON[@"result"]];
        
        [self.leftClassTableView  reloadData];
        [self.rigthClassTableView reloadData];
        
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    
}

#pragma MARK - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView == _leftClassTableView){
        
        NSLog(@"%zd", self.arrayM.count);
        return self.arrayM.count;
    } else {
        if (self.arrayM.count > 0) {
            LWbigCategoryModel *oneCategoryModel = [self.arrayM objectAtIndex:self.categoryIndex];
            NSLog(@"%zd", oneCategoryModel.twoCategory.count);
            return oneCategoryModel.twoCategory.count;
        } else {
            return 0;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _leftClassTableView) {
        static NSString *ID = @"_leftClassTableViewCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        
        LWbigCategoryModel *oneCategoryModel = self.arrayM[indexPath.row];
        cell.textLabel.text = oneCategoryModel.mobile_name;
        
        return cell;
    }
    
    else {
        static NSString *ID = @"_RightClassTableViewCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        
        LWbigCategoryModel *oneCategoryModel = [self.arrayM objectAtIndex:self.categoryIndex];
        
        NSLog(@"%@", oneCategoryModel);
        
        
        LWsmallCategoryModel *twoCategoryModel = oneCategoryModel.twoCategory[indexPath.row];
        
        cell.textLabel.text = twoCategoryModel.mobile_name;
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _leftClassTableView) {
        return 64;
    } else {
        return 64;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == _leftClassTableView) {
        // 点击的分类
        self.categoryIndex = indexPath.row;
        [self.rigthClassTableView reloadData];
    } else{
        // 点击详情
        
        LWorderListViewController *listVc = [LWorderListViewController new];
        
        listVc.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:listVc animated:YES];
        
        
    }
}

@end
